package com.woorigil.social.domain;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ProviderUtils {
	public static String webViewPage(ProviderCode providerCode, String metaId) {
		String url = null;
		if (providerCode == ProviderCode.CP) {
			url = "http://www.coupang.com/deal.pang?coupang=" + metaId;
		} else if (providerCode == ProviderCode.WM) {
			url = "http://www.wemakeprice.com/deal/adeal/" + metaId;
		} else if (providerCode == ProviderCode.GP) {
			url = "http://www.groupon.kr/app/product/pview/" + metaId;
		} else if (providerCode == ProviderCode.OC) {
			url = "http://www.oclock.co.kr/prd/detail_cate.jsp?chn_cd=30001016&item_cd=" + metaId;
		} else if (providerCode == ProviderCode.TM) {
			url = "http://www.ticketmonster.co.kr/deal/" + metaId;
		}
		return url;
	}
	
	public static String getQnaPage(ProviderCode providerCode, String metaId) throws IOException {
		String url = null;
		if (providerCode == ProviderCode.CP) {
			url = getQnaUrl("http://m.coupang.com/m/deal.pang?coupang=" + metaId);
		} else if (providerCode == ProviderCode.WM) {
			url = "http://m.wemakeprice.com/m/deal/get_article?mode=qna&deal_id=" + metaId;
		} else if (providerCode == ProviderCode.GP) {
			url = "http://www.groupon.kr/app/product/pview/" + metaId;
		} else if (providerCode == ProviderCode.OC) {
			url = "http://www.oclock.co.kr/prd/detail_cate.jsp?chn_cd=30001016&item_cd=" + metaId;
		} else if (providerCode == ProviderCode.TM) {
			url = "http://wvvw.ticketmonster.co.kr/talk/" + metaId + "#content_start";
		}
		return url;
	}
	
	private static String getQnaUrl(String viewUrl) throws IOException {
		// /m/comment.pang?documentSrl=40546779&cate=1&coupangSrl=40546780",
		Document doc = Jsoup.connect(viewUrl).timeout(1000).get();
		String html = doc.html();
		int startIndex = html.indexOf("/m/comment.pang?documentSrl=");
		String str = html.substring(startIndex, startIndex + 80);
		String uri = str.substring(0, str.indexOf("\""));
		return "http://m.coupang.com" + uri;
	}

	public static String getDetailPage(ProviderCode providerCode, String metaId) {
		if (providerCode == ProviderCode.TM) {
			return "http://m.ticketmonster.co.kr/deal/detailDaily/" + metaId;
		}
		return null;
	}

	public static ProviderCode getValueByEmail(String from) {
		if ("noreply@e.coupang.com".equals(from)) {
			return ProviderCode.CP;
		} else if ("noreply@ticketmonster.co.kr".equals(from)) {
			return ProviderCode.TM;
		} else if ("no-reply@wemakeprice.com".equals(from)) {
			return ProviderCode.WM;
		} else if ("noreply@groupon.kr".equals(from)) {
			return ProviderCode.GP;
		} else if ("master@cjmall.com".equals(from)) {
			return ProviderCode.OC;
		}
		return null;
	}	
}
